﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace TcpServer
{
    class Program
    {
        /// <summary>
        /// 服务端，监听IP
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            TcpListener listener = new TcpListener(ip, 7890);
            listener.Start();

            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();//接受一个Client  

                byte[] buffer = new byte[client.ReceiveBufferSize];

                NetworkStream stream = client.GetStream();//获取网络流  

                stream.Read(buffer, 0, buffer.Length);//读取网络流中的数据  

                stream.Close();//关闭流  

                client.Close();//关闭Client  

                string receiveString = Encoding.Default.GetString(buffer).Trim('\0');//转换成字符串  

                Console.WriteLine(receiveString);

            }
            listener.Stop();
        }
    }
}
